package com.suning.sawp.member.dao.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.suning.sawp.dto.storeman.Category;
import com.suning.sawp.member.dao.MemExpandConversionDao;
import com.suning.sawp.member.dto.MemExpandDataOfPlaceDto;
import com.suning.sawp.member.vo.MemExpandDataOfCateVo;
import com.suning.sawp.member.vo.MemExpandDataOfEmployeeVo;
import com.suning.sawp.member.vo.MemExpandDataOfStoreVo;

/**
 * 会员拓展销售转换数据Dao接口实现 〈一句话功能简述〉<br>
 * 〈功能详细描述〉
 *
 * @author 15050519
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service(value = "memExpandConversionDao")
public class MemExpandConversionDaoImpl extends BaseDaoImpl implements MemExpandConversionDao {

    /**
     * 根据活动ID查询门店活动的拓展转换数据
     */
    @Override
    public List<MemExpandDataOfStoreVo> queryExpandDataOfStore(Long activityId, Integer source, Integer mode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("activityId", activityId);
        // 客户来源不为空,0:门店外拓;1:询价未购
        if (null != source) {
            paramMap.put("source", source);
        }
        // 拓展方式不为空,0:预充值;1:意向
        if (null != mode) {
            paramMap.put("mode", mode);
        }
        return dalClientRead.queryForList("MEM_EXPAND_CONVERSION.QUERY_EXPAND_DATA_STORE", paramMap,
                MemExpandDataOfStoreVo.class);
    }

    /**
     * 根据活动ID查询门店品类活动的拓展转换数据
     */
    @Override
    public List<MemExpandDataOfCateVo> queryExpandDataOfStoreCate(Long activityId, String storeCode, Integer source,
            Integer mode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("activityId", activityId);
        paramMap.put("storeCode", storeCode);
        // 客户来源不为空,0:门店外拓;1:询价未购
        if (null != source) {
            paramMap.put("source", source);
        }
        // 拓展方式不为空,0:预充值;1:意向
        if (null != mode) {
            paramMap.put("mode", mode);
        }
        return dalClientRead.queryForList("MEM_EXPAND_CONVERSION.QUERY_EXPAND_DATA_STORE_CATE", paramMap,
                MemExpandDataOfCateVo.class);
    }

    /**
     * 根据活动ID查询门店品类下员工活动的拓展转换数据
     */
    @Override
    public List<MemExpandDataOfEmployeeVo> queryExpandDataOfEmployee(Long activityId, String storeCode, String cateCode,
            Integer source, Integer mode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("activityId", activityId);
        paramMap.put("storeCode", storeCode);
        paramMap.put("cateCode", cateCode);
        // 客户来源不为空,0:门店外拓;1:询价未购
        if (null != source) {
            paramMap.put("source", source);
        }
        // 拓展方式不为空,0:预充值;1:意向
        if (null != mode) {
            paramMap.put("mode", mode);
        }
        return dalClientRead.queryForList("MEM_EXPAND_CONVERSION.QUERY_EXPAND_DATA_EMPLOYEE", paramMap,
                MemExpandDataOfEmployeeVo.class);
    }

    /**
     * 根据时间查询门店指定时间内外拓地点销售转换数据
     */
    @Override
    public List<MemExpandDataOfPlaceDto> queryExpandDataOfPlace(String storeCode, Date startDate, Date endDate, String placeName) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("storeCode", storeCode);
        paramMap.put("startDate", startDate);
        paramMap.put("endDate", endDate);
        // 地点名称不为空,模糊查询
        if (StringUtils.isNotBlank(placeName)) {
            paramMap.put("placeName", "%" + placeName + "%");
        }
        return dalClientRead.queryForList("MEM_EXPAND_CONVERSION.QUERY_EXPAND_DATA_PLACE", paramMap,
                MemExpandDataOfPlaceDto.class);
    }

    /**
     * 处理销售转换数据
     */
    @Override
    public void processExpandConversionData(Date endDate) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        if (null != endDate) {
            paramMap.put("endDate", endDate);
            
            // 更新门店销售转换
            dalClient.execute("MEM_EXPAND_CONVERSION.REPLACE_INTO_SALE_STORE", paramMap);
            
            // 更新门店品类销售转换
            dalClient.execute("MEM_EXPAND_CONVERSION.REPLACE_INTO_SALE_STORE_CATE", paramMap);
            
            // 更新门店品类下员工销售转换 -1
            dalClient.execute("MEM_EXPAND_CONVERSION.REPLACE_INTO_SALE_STORE_EMPLOYEE", paramMap);
            
            // 删除门店拓展地点销售转换
            dalClient.execute("MEM_EXPAND_CONVERSION.DELETE_SALE_STORE_PLACE", paramMap);
            
            // 更新门店拓展地点销售转换
            dalClient.execute("MEM_EXPAND_CONVERSION.INSERT_INTO_SALE_STORE_PLACE", paramMap);
        }
    }

    /**
     * 查询门店拓展品类
     */
    @Override
    public List<Category> queryActivityStoreCategorys(Long activityId, String storeCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("storeCode", storeCode);
        paramMap.put("activityId", activityId);
        return dalClientRead.queryForList("MEM_EXPAND_CONVERSION.QUERY_ACTIVITY_STORE_CATEGORYES", paramMap,
                Category.class);
    }
}
